package com.aaa.utils;

import java.sql.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ：肖成杰（1752167034@qq.com）
 * @date ：Created in 2022/8/8 14:13
 * @description：basefao工具类
 * @modified By：
 * @version:
 */
public class BaseDao {
    static private String driver = "com.mysql.cj.jdbc.Driver";
    static private String url = "jdbc:mysql://127.0.0.1:3306/mysql_xcj?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai";
    static private String username = "root";
    static private String password = "root";

    static {
        try {
            Class.forName(driver);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    private Connection getConnection() {
        Connection connection = null;
        try {
            connection = DriverManager.getConnection(url, username, password);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return connection;
    }

    private void setPre(PreparedStatement ps, Object... params) {
        if (params != null && params.length > 0) {
            for (int i = 0; i < params.length; i++) {
                try {
                    ps.setObject(i + 1, params[i]);
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private void closeAll(ResultSet rs, PreparedStatement ps, Connection con) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (ps != null) {
            try {
                ps.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (con != null) {
            try {
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public boolean executeUpdate(String sql, Object... params) {
        Connection con = this.getConnection();
        PreparedStatement ps = null;
        try {
            ps = con.prepareStatement(sql);
            setPre(ps, params);
            int i = ps.executeUpdate();
            if (i > 0) {
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll(null, ps, con);
        }
        return false;
    }

    public List<Map<String,Object>> executeQuery(String sql, Object... params) {
        Connection con = this.getConnection();
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = con.prepareStatement(sql);
            setPre(ps, params);
            rs = ps.executeQuery();
            int columnCount = rs.getMetaData().getColumnCount();
            List<Map<String,Object>> list = new ArrayList<>();
            while (rs.next()) {
                Map map = new HashMap<>();
                for (int i = 1; i <= columnCount; i++) {
                    String keyname = rs.getMetaData().getColumnName(i);
                    Object value = rs.getObject(keyname);
                    map.put(keyname, value);
                }
                list.add(map);
            }
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll(rs, ps, con);
        }
        return null;
    }
}